package com.hsc.www;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class MyController {
    @Autowired
    UserService userService;

    @RequestMapping(value = {"/"})
    public String welcomePage(Map map) {
        map.put("userList",userService.findAllUsers());
        return "index";
    }


    @Secured({ "ROLE_Guest","ROLE_Admin" })
    @RequestMapping(value = "insertUserPage")
    public String addUserPage(Map map) {
        map.put("submitUrl","insertUser");
        return "editUser";
    }


    @Secured({ "ROLE_Guest","ROLE_Admin" })
    @RequestMapping(value = "insertUser")
    public String insertUser(User user,Map map) {
       int result= userService.insertUser(user);
       if (result==1){
           return  "redirect:/";
       }else{
           map.put("message", "新增失败");
           return  "error/errorPage";
       }
    }


    @PreAuthorize("hasRole('Guest') AND hasRole('Admin')")
    @RequestMapping(value = "deleteUserById")
    public String deleteUserById(Integer id,Map map) {
       int result= userService.deleteUserById(id);
       if (result==1){
           return  "redirect:/";
       }else{
           map.put("message", "删除失败");
           return  "error/errorPage";
       }
    }




}
